package cn.vinord.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.vinord.Properties.Properties;
import me.chanjar.weixin.common.exception.WxErrorException;

@RestController
public class SendTemplateController {
	
	 @Autowired
	 private WxMaService service;
	 
	 @Autowired
	 private Properties properties;
	 
	@GetMapping("/openId/{js_code}")
	public JSONObject getOpenid(@PathVariable String js_code) {
		try {
			String result = service.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + properties.getWxMaProperties().getAppid() + "&secret=" +  properties.getWxMaProperties().getSecret() + "&js_code=" + js_code + "&grant_type=authorization_code", null);
			return JSONObject.parseObject(result);
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return null;
	}

}
